package com.example.com.skills_utf8;

import android.app.Fragment;
import android.app.usage.UsageEvents.Event;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;



//http://blog.csdn.net/bigconvience/article/details/26735705
// http://blog.csdn.net/bigconvience/article/details/26391743
// http://blog.csdn.net/ldwtill/article/details/10162799   android屏幕触摸机制
public class _58_2Fragment extends Fragment{
	
	VelocityTracker mVelocityTracker;
	private TextView tv_log;
	private ImageView iv_target;

	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		View view = inflater.inflate(R.layout.fragment_58_2, null);
		
		tv_log = (TextView)view.findViewById(R.id.tv_log);
		iv_target = (ImageView)view.findViewById(R.id.iv_target);
  		
		
		iv_target.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				
				switch(event.getAction()){
				case MotionEvent.ACTION_DOWN:

					 if (mVelocityTracker == null) { 
				            mVelocityTracker = VelocityTracker.obtain();//获得VelocityTracker类实例 
					 }
					 else{	
						 mVelocityTracker.clear();
					 }
					 
					mVelocityTracker.addMovement(event);
					break;
					
				case MotionEvent.ACTION_CANCEL:
				case MotionEvent.ACTION_UP:
					mVelocityTracker.computeCurrentVelocity(1000);	
					float velocity_x = mVelocityTracker.getXVelocity();	// 有正负之分详情见引用的url
					float velocity_y = mVelocityTracker.getYVelocity();
					
					String str = "velocity_x: "+velocity_x + "velocity_y: "+velocity_y;
					 
					tv_log.setText(str);
					break;
					
				case MotionEvent.ACTION_MOVE:
					mVelocityTracker.addMovement(event);
					break;
					
				 
				}
				 
				
				return true;
			}
		});
		
		
		
		return view;
		
	}
	
	
	
	
}
